package com.spsoft.dmzeinvoice.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.spsoft.core.enums.ResponseEnum;
import com.spsoft.dmzeinvoice.model.vo.LeqiResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Type;

@Slf4j
public class LeqiResultUtil {
    /**
     * 乐企接口返回json串，转换成type
     * @param jsonStr 乐企返回的json字符串
     * @param type Type type = new TypeToken<LeqiResult<T>>(){}.getType();T 是具体的某个data类，如VmsEnterpriseInfoVO
     * @return LeqiResult对象
     */
    public static LeqiResult jsonToLeqiResult(String jsonStr, Type type) {
        if (StringUtils.isBlank(jsonStr)) {
            LeqiResult result = new LeqiResult();
            LeqiResult.ResponseInfo responseInfo = new LeqiResult.ResponseInfo();
            LeqiResult.ErrorInfo errorInfo = new LeqiResult.ErrorInfo();
            errorInfo.setCode(ResponseEnum.LEQI_RETURN_NULL_ERROR.getCode());
            errorInfo.setMessage(ResponseEnum.LEQI_RETURN_NULL_ERROR.getMsg());
            responseInfo.setError(errorInfo);
            result.setResponse(responseInfo);
            return result;
        }
        return JSONObject.parseObject(jsonStr, type);
    }
}
